package com.yucc.girlimage.ui;

import android.app.Dialog;
import android.app.Service;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Vibrator;

import com.yucc.girlimage.R;

public class ShakeActivity extends BaseActivity implements SensorEventListener {
	private static final int DIALOG_DISH_DETAIL = 1;
	
	// 定义sensor管理器
	private SensorManager mSensorManager;
	// 震动
	private Vibrator vibrator;
	
	@Override
	protected int _onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_shake) ;

		// 获取传感器管理服务
		mSensorManager = (SensorManager) getSystemService(Service.SENSOR_SERVICE);
		// 震动
		vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
		
		setupView() ;
		
		return ACTION_SET_BACKGROUND_AND_RECEIVER;
	}
	
	private void setupView(){
		initTitleBar() ;
	}
	
	private void initTitleBar(){
		
	}

	@Override
	protected void onResume() {
		super.onResume();

		// 加速度传感器
		mSensorManager.registerListener(this,
				mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
				// 还有SENSOR_DELAY_UI、SENSOR_DELAY_FASTEST、SENSOR_DELAY_GAME等，
				// 根据不同应用，需要的反应速率不同，具体根据实际情况设定
				SensorManager.SENSOR_DELAY_NORMAL);
	}

	@Override
	protected void onStop() {
		mSensorManager.unregisterListener(this);
		super.onStop();
	}

	@Override
	protected void onPause() {
		mSensorManager.unregisterListener(this);
		super.onPause();
	}
	
	@Override
	protected Dialog onCreateDialog(int id, Bundle args) {
//		switch (id) {
//			case DIALOG_DISH_DETAIL:
//				final Shop shop = args.getParcelable("shop") ;
//				AlertDialog.Builder builder = new Builder(this).setMessage(
//						shop.name).setPositiveButton("查看", new OnClickListener() {
//							
//							@Override
//							public void onClick(DialogInterface dialog, int which) {
//								Intent intent = new Intent(ShakeActivity.this,ShopDetailActivity.class) ;
//								intent.putExtra("shop", shop);
//								startActivity(intent);
//								
//							}
//						}).setNegativeButton("再摇一次", null);
//				return builder.create() ;
//		}
		return super.onCreateDialog(id, args);
	}
	
	@Override
	protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
		switch (id) {
			case DIALOG_DISH_DETAIL:
//				if(args != null){
//					Shop shop = args.getParcelable("shop") ;
//					((AlertDialog)dialog).setMessage(shop.name) ;
//				}
				break ;
			}
		super.onPrepareDialog(id, dialog, args);
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub
		// 当传感器精度改变时回调该方法，Do nothing.
	}

	@Override  
	public void onSensorChanged(SensorEvent event) {  
	  // TODO Auto-generated method stub  
	  int sensorType = event.sensor.getType();  
	  
	  //values[0]:X轴，values[1]：Y轴，values[2]：Z轴  
	  float[] values = event.values;  
	  
	  if(sensorType == Sensor.TYPE_ACCELEROMETER){  
	  
		  /*因为一般正常情况下，任意轴数值最大就在9.8~10之间，只有在你突然摇动手机 
		  *的时候，瞬时加速度才会突然增大或减少。 
		  *所以，经过实际测试，只需监听任一轴的加速度大于14的时候，改变你需要的设置 
		  *就OK了~~~ 
		  */  
		   if( Math.abs(values[0]) > 14 ||  Math.abs(values[1]) > 14 || Math.abs(values[2])>14 ){  
		  
			    //摇动手机后，再伴随震动提示~~  
			    vibrator.vibrate(500);  
			    
//			    showShakeResult(getRandomShop()) ;
		  
		   }  
	  }  
	}
	
//	private void showShakeResult(Shop shop){
//		Bundle bundle = new  Bundle() ;
//		bundle.putParcelable("shop", shop);
//		showDialog(DIALOG_DISH_DETAIL, bundle) ;
//	}
	
	
//	private Shop getRandomShop(){
//		if(mShops == null){
//			mShops = ShopProvider.getShops(this) ;
//		}
//		
//		return mShops.get(new Random().nextInt(mShops.size())) ;
//	}
}
